﻿package core.ui.controls
{
    import core.event.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;

    public class Slider extends Sprite
    {
        var drag:Sprite;
        var size:int = 100;

        public function Slider(param1:int, param2:int = 100)
        {
            this.size = param1;
            this.drag = new IconButton(null, "", new SlidebarDragOut());
            this.graphics.beginFill(14935011);
            this.graphics.drawRect(1, 10, (param1 - 1), 4);
            this.graphics.beginFill(9079434);
            this.graphics.drawRect(0, 10, (param1 - 1), 3);
            this.graphics.beginFill(13882323);
            this.graphics.drawRect(1, 11, param1 - 2, 2);
            this.graphics.endFill();
            this.addEventListener(MouseEvent.MOUSE_DOWN, this.MiddleMouseDown, false, 1, true);
            this.drag.y = 3;
            this.drag.x = Math.round(param1 * (param2 / 100) - 4);
            this.drag.addEventListener(MouseEvent.MOUSE_DOWN, this.DragMouseDown, false, 0, true);
            addChild(this.drag);
            return;
        }// end function

        public function Get() : Number
        {
            return (this.drag.x + 4) / this.size;
        }// end function

        public function Add(round:Number) : void
        {
            this.drag.x = Math.round(this.drag.x + round * this.size);
            if (this.drag.x > this.size - 4)
            {
                this.drag.x = this.size - 4;
            }
            this.DragMouseMove(null);
            return;
        }// end function

        public function Remove(round:Number) : void
        {
            this.drag.x = Math.round(this.drag.x - round * this.size);
            if (this.drag.x < -4)
            {
                this.drag.x = -4;
            }
            this.DragMouseMove(null);
            return;
        }// end function

        public function Set(round:Number, round:Boolean = true) : void
        {
            if (round < 0)
            {
                round = 0;
            }
            else if (round > 1)
            {
                round = 1;
            }
            this.drag.x = Math.round(this.size * round - 4);
            if (round)
            {
                this.DragMouseMove(null);
            }
            return;
        }// end function

        private function DragMouseMove(event:Event) : void
        {
            dispatchEvent(new SliderEvent(SliderEvent.DRAG, (this.drag.x + 4) / this.size, event == null));
            return;
        }// end function

        function DragMouseDown(event:MouseEvent) : void
        {
            this.drag.startDrag(false, new Rectangle(-4, 3, this.size, 0));
            stage.addEventListener(MouseEvent.MOUSE_MOVE, this.DragMouseMove, false, 1, true);
            stage.addEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, false, 1, true);
            return;
        }// end function

        function DragMouseUp(event:MouseEvent) : void
        {
            this.drag.stopDrag();
            stage.removeEventListener(MouseEvent.MOUSE_MOVE, this.DragMouseMove, false);
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, false);
            dispatchEvent(new SliderEvent(SliderEvent.DRAG, (this.drag.x + 4) / this.size, event == null));
            dispatchEvent(new SliderEvent(SliderEvent.RELEASE, (this.drag.x + 4) / this.size));
            return;
        }// end function

        private function MiddleMouseDown(event:MouseEvent) : void
        {
            if (this.mouseX > 0 && this.mouseX < this.size)
            {
                this.drag.x = Math.round(this.mouseX - 4);
                this.DragMouseDown(null);
            }
            return;
        }// end function

    }
}
